package com.mejlisse.gwt.scaffold;

import com.google.gwt.app.place.Activity;
import com.google.gwt.app.place.ActivityMapper;
import com.google.gwt.app.place.Place;
import com.google.gwt.app.place.PlaceController;
import com.google.gwt.app.place.ProxyPlace;
import com.mejlisse.gwt.request.ApplicationRequestFactory;
import com.mejlisse.gwt.request.ApplicationEntityTypesProcessor;
import com.mejlisse.gwt.request.NewsRecord;
import com.mejlisse.gwt.ui.NewsActivitiesMapper;
import com.mejlisse.gwt.request.CommentRecord;
import com.mejlisse.gwt.ui.CommentActivitiesMapper;

/**
 * Instantiates detail activities.
 */
public class ApplicationDetailsActivities implements ActivityMapper {
	private final ApplicationRequestFactory requests;
	private final PlaceController placeController;

	public ApplicationDetailsActivities(ApplicationRequestFactory requestFactory, PlaceController placeController) {
		this.requests = requestFactory;
		this.placeController = placeController;
	}

	public Activity getActivity(Place place) {
		if (!(place instanceof ProxyPlace)) {
			return null;
		}

		final ProxyPlace proxyPlace = (ProxyPlace) place;

		return new ApplicationEntityTypesProcessor<Activity>() {
			@Override
			public void handleNews(NewsRecord proxy) {
				setResult(new NewsActivitiesMapper(requests, placeController).getActivity(proxyPlace));
			}
			@Override
			public void handleComment(CommentRecord proxy) {
				setResult(new CommentActivitiesMapper(requests, placeController).getActivity(proxyPlace));
			}
		}.process(proxyPlace.getProxy());
	}
}
